﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 账号管理
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtName.Text.IsNullOrEmpty())
            {
                MessageBox.Show("请输入用户名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            IDatabase db = DatabaseFactory.CreateDatabase("sqlite");
            string password = db.ExecuteScalar<string>("select 密码 from 用户 where 用户=@user and 是否启用='true'", new { user = txtName.Text });
            if (XEncrypt.DES_Encrypt(txtPwd.Text) == password || txtPwd.Text == password)
            {
                Function.CacheUser = new UserData { UserName = txtName.Text, UserPass = txtPwd.Text, Enabled = true };
                XSerialize.SerializeToFile<string>(Function.CacheUser.UserName, Path.Combine(Application.StartupPath, "u.dat"));
                FormMain main = new FormMain();
                main.Show();
                this.Hide();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            string uFile=Path.Combine(Application.StartupPath, "u.dat");
            if (File.Exists(uFile))
            {
                txtName.Text = XSerialize.DeserializeFromFile<string>(uFile);
                txtPwd.Focus();
            }
        }
    }
}
